package com.doit.demo07;

import java.util.ArrayList;
import java.util.List;

//线程通信
public class test {
    public static void main(String[] args) {
        List<String>list =new ArrayList<>();
        Object obj = new Object();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true)
                {
                    synchronized (obj)
                    {
                        if(list.size()!=0)//如果有数据,等待
                        {
                            try {
                                obj.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        else{
                            list.add("aaa");//添加并唤醒
                            System.out.println(list);
                            obj.notify();
                        }
                    }

                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    synchronized (obj)
                    {
                        if(list.size()==0)//如果没有数据,等待
                        {
                            try {
                                obj.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        else
                        {

                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }


                            String remove = list.remove(0);
//                            System.out.println(remove);
                            System.out.println(list);
                            obj.notify();
                        }

                    }
                }
            }
        }).start();

    }
}
